package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.SetmealDao;
import com.itheima.domain.Category;
import com.itheima.domain.R;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public void addSetmeal(SetmealDto setmealDto) {
        //保存套餐到setmeal表中
        setmealService.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //更新到数据库
        setmealDishService.saveBatch(setmealDishes);
    }

    //套餐条件分页查询
    @Override
    public Page<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {
        //分页查询
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wapper = new LambdaQueryWrapper<>();
        //设置条件
        if(name!= null && name!=""){
            wapper.like(Setmeal::getName,name);
        }
        Page<Setmeal> page1 = this.page(pageInfo, wapper);

        List<Setmeal> setmeals = page1.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();
        for (Setmeal setmeal : setmeals) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            //查询分类名字
            Category category = categoryService.getById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            setmealDtos.add(setmealDto);
        }
        Page<SetmealDto> dtoPage = new Page<>();
        dtoPage.setRecords(setmealDtos);//数据集合
        dtoPage.setTotal(page1.getTotal());//设置总条数
        //返回
        return dtoPage;
    }

    //套餐批量逻辑删除
    @Override
    public void delectIds(Long[] ids) {
        List<Setmeal> setmeals = this.listByIds(Arrays.asList(ids));//数组强转为集合
        //循环修改所有套餐的is_deleted属性
        setmeals.stream().map(c->{c.setIsDeleted(1);
        return c;
        }).collect(Collectors.toList());
        //只需要修改
        this.updateBatchById(setmeals);

        //修改菜品信息
        for (Setmeal setmeal : setmeals) {
            LambdaQueryWrapper<SetmealDish> wapper = new LambdaQueryWrapper<>();
            wapper.eq(SetmealDish::getSetmealId,setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishService.list(wapper);
            setmealDishList.stream().map(c->{c.setIsDeleted(1);
            return c;
            }).collect(Collectors.toList());
            //更新套餐菜品数据
            setmealDishService.updateBatchById(setmealDishList);
        }
    }

    //根据分类ID查询套餐
    @Override
    public List<Setmeal> findList(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> wapper = new LambdaQueryWrapper<>();
        if(setmeal.getCategoryId()!=null){
            wapper.eq(Setmeal::getCategoryId,setmeal.getCategoryId());
        }
        wapper.eq(Setmeal::getStatus,1);
        List<Setmeal> list = setmealService.list(wapper);
        return list;
    }

    //套餐批量停售
    @Override
    public R modify(Long[] ids) {
        List<Long> list = JSON.parseArray(JSON.toJSONString(ids), Long.class);
        List<Setmeal> setmeals = this.listByIds(list);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(0);
            this.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    //套餐批量起售
    @Override
    public R modify1(Long[] ids) {
        List<Long> list = JSON.parseArray(JSON.toJSONString(ids), Long.class);
        List<Setmeal> setmeals = this.listByIds(list);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(1);
            this.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    //套餐修改  回显功能
    @Override
    public SetmealDto setmealDtoById(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> wapper = new LambdaQueryWrapper<>();
        wapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> dishList = setmealDishService.list(wapper);
        setmealDto.setSetmealDishes(dishList);

        return setmealDto;
    }

    //套餐修改功能
    @Override
    public void updateSetmealDto(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //strearm 流为每个数据设置id
        setmealDishes.stream().map(c->{c.setSetmealId(setmealDto.getId());
        return c;
        }).collect(Collectors.toList());
        //先删除
        LambdaUpdateWrapper<SetmealDish> wapper = new LambdaUpdateWrapper<>();
        wapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(wapper);
        //再添加
        setmealDishService.saveOrUpdateBatch(setmealDishes);
    }
}
